import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from 'views/Login'
import Register from 'views/Register'
import SellerRegister from 'views/SellerRegister'
import User from 'views/User'
import UserWallet from 'components/UserWallet'
import UserOrder from 'components/UserOrder'
import ExpressOrder from 'components/ExpressOrder'
import PurchaseOrder from 'components/PurchaseOrder'
import UserInformation from 'components/UserInformation'
import SellerInformation from 'components/SellerInformation'
import SellerOrder from 'components/SellerOrder'
import UserAddress from 'components/UserAddress'
import EditPassword from 'components/EditPassword'
import Seller from 'views/Seller'
import SellerWallet from 'components/SellerWallet'
import SellerShop from 'components/SellerShop'
import Courier from 'views/Courier'
import Home from 'views/Home'
import Shop from 'views/Shop'
import Stores from 'views/Stores'
import Store from 'views/Store'
import Order from 'views/Order'
import Products from 'views/Products'
import ErrorPage from 'views/Error'
import Test from 'views/Test'
import userService from 'services/userService.js'
import { MessageBox } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: Home, meta: { requiresAuth: false } },
  { path: '/login', component: Login, meta: { requiresAuth: false } },
  { path: '/register', component: Register, meta: { requiresAuth: false } },
  { path: '/sellerRegister', component: SellerRegister, meta: { requiresAuth: false } },
  { path: '/home', component: Home, meta: { requiresAuth: false } },
  {
    path: '/user',
    component: User,
    children: [
      { path: '', component: UserWallet, meta: { requiresAuth: true } },
      { path: 'wallet', component: UserWallet, meta: { requiresAuth: true } },
      { path: 'expressorder', component: ExpressOrder, meta: { requiresAuth: true } },
      { path: 'purchaseorder', component: PurchaseOrder, meta: { requiresAuth: true } },
      { path: 'userinformation', component: UserInformation, meta: { requiresAuth: true } },
      { path: 'useraddress', component: UserAddress, meta: { requiresAuth: true } },
      { path: 'editpassword', component: EditPassword, meta: { requiresAuth: true } },
      { path: 'orders', component: UserOrder, meta: { requiresAuth: true } }
    ],
    meta: { requiresAuth: true } },
  {
    path: '/seller',
    component: Seller,
    children: [
      { path: '', component: SellerShop, meta: { requiresAuth: true } },
      { path: 'wallet', component: SellerWallet, meta: { requiresAuth: true } },
      { path: 'shop', component: SellerShop, meta: { requiresAuth: true } },
      { path: 'information', component: SellerInformation, meta: { requiresAuth: true } },
      { path: 'orders', component: SellerOrder, meta: { requiresAuth: true } }
    ],
    meta: { requiresAuth: true }
  },
  { path: '/shop', component: Shop, meta: { requiresAuth: true } },
  { path: '/stores', component: Stores, meta: { requiresAuth: true } },
  { path: '/store', component: Store, meta: { requiresAuth: true } },
  { path: '/products', component: Products, meta: { requiresAuth: true } },
  { path: '/order', component: Order, meta: { requiresAuth: true } },
  { path: '/courier', component: Courier, meta: { requiresAuth: true } },
  { path: '/test', component: Test, meta: { requiresAuth: false } },
  { path: '*', component: ErrorPage, meta: { requiresAuth: false } }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !userService.isLogin()) {
    MessageBox.alert('您还未登录或登录状态已失效，请登录后继续操作', '未登录', {
      confirmButtonText: '返回登录',
      callback: action => {
        next('/login')
      }
    })
  } else {
    next()
  }
})

export default router
